//go:build ignore

package main

import (
	"fmt"
	"io"
	"net/http"
	"sync"
	"time"
)

var t int = 0
var lk sync.Mutex

func testapi(i int) {
	//fmt.Println(i)
	requestUrl := "http://127.0.0.1:9999/api/files"

	start := time.Now()
	response, err := http.Get(requestUrl)
	if err != nil {
		fmt.Println(err.Error())
		//ch <- false
		return
	}

	defer func(Body io.ReadCloser) {
		err := Body.Close()
		if err != nil {
			fmt.Println(err.Error())
			//ch <- false
			return
		}
	}(response.Body)

	if time.Since(start).Seconds() > 3 {
		//fmt.Println("timeout")
		lk.Lock()
		t++
		lk.Unlock()
		//ch <- false
	}
	defer w.Done()
}

func testhandle(i int) {
	//fmt.Println(i)
	requestUrl := "http://127.0.0.1:9999/api/users"

	start := time.Now()
	response, err := http.Get(requestUrl)
	if err != nil {
		fmt.Println(err.Error())
		//ch <- false
		return
	}

	defer func(Body io.ReadCloser) {
		err := Body.Close()
		if err != nil {
			fmt.Println(err.Error())
			//ch <- false
			return
		}
	}(response.Body)

	if time.Since(start).Seconds() > 3 {
		//fmt.Println("timeout")
		lk.Lock()
		t++
		lk.Unlock()
		//ch <- false
	}
	defer w.Done()
}

var ch = make(chan bool)
var w sync.WaitGroup

func main() {
	sum := 2000
	for cur := 0; cur < 5; cur++ {
		t = 0
		w.Add(sum)
		for i := 0; i < sum; i++ {
			go testhandle(i)
		}
		w.Wait()
		//fmt.Println("end: ", <-ch)
		fmt.Println("sum: ", sum-t)
		if t == 0 {
			sum += 200
		}

		//sum += 100 * cur
	}
}
